package com.ling.core.view.builder;

import com.bstek.dorado.view.widget.base.Panel;
import com.ling.core.view.ViewComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class PanelBuilder extends AbstractControlBuilder {

    public void build(Object control, ViewComponent parentViewComponent) {
        Panel panel = (Panel) control;
        String id = panel.getId();
        ViewComponent component = null;
        if (StringUtils.isNotEmpty(id)) {
            component = this.generateViewComponent(id, Panel.class);
            if (StringUtils.isNotEmpty(panel.getCaption())) {
                component.setDesc(panel.getCaption());
            }
        } else if (StringUtils.isNotEmpty(panel.getCaption())) {
            component = this.generateViewComponent(panel.getCaption(), Panel.class);
        }

        if (component != null) {
            if (StringUtils.isEmpty(component.getId())) {
                component.setEnabled(false);
            }
            parentViewComponent.addChildren(component);
        }

        for (com.bstek.dorado.view.widget.Component c : panel.getChildren()) {
            for (IControlBuilder builder : builders) {
                if (builder.support(c)) {
                    builder.build(c, component);
                    break;
                }
            }
        }

        for (com.bstek.dorado.view.widget.Component c : panel.getButtons()) {
            for (IControlBuilder builder : builders) {
                if (builder.support(c)) {
                    builder.build(c, component);
                    break;
                }
            }
        }
        parentViewComponent.addChildren(component);
    }

    public boolean support(Object control) {
        return control instanceof Panel;
    }

}
